//https://www.dotcpp.com/oj/contest6265_problem0.html
package 题目集.dfs;

import java.util.Scanner;

public class 坦克战车 {
    static boolean[][] map;
    static int sx, sy, ex, ey;
    static int n;
    static int[][] dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    static boolean[][] visited = new boolean[100][100];

    public static int dfs(int x, int y) {
        int res=Integer.MAX_VALUE;
        for (int[] ints : dir) {
            int toX = x + ints[0];
            int toY = y + ints[1];
            if (toX < 0 || toX >=n || toY < 0 || toY >= n|| visited[toX][toY]) {    //越界或者已经访问过
                continue;
            }
            if (toX == ex && toY == ey) {
                return 1;
            }
            if ((sx == x && sy == y) || map[toX][toY] ^ map[x][y]) {    //异性或者是起点
                visited[toX][toY] = true;
                int dfs = dfs(toX, toY);
                if (dfs != Integer.MAX_VALUE) {
                    res = Math.min(res, dfs + 1);
                }
                visited[toX][toY] = false;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        n = sc.nextInt();
        map = new boolean[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                String v = sc.next();
                map[i][j] = v.equals("+");
                if (v.equals("A")) {
                    sx = i;
                    sy = j;
                }else if (v.equals("B")) {
                    ex = i;
                    ey = j;
                }
            }
        }
        int res = dfs(sx, sy);
        System.out.println(res);
    }
}
